package com.bestcem.xm.member.util;

import cn.hutool.core.util.ObjectUtil;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.member.controller.param.HttpMemberFieldParam;
import com.bestcem.xm.member.enums.ExtendFieldTypeEnum;

/**
 * 扩展字段工具类
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/7/19
 */
public class FieldUtil {

    /**
     * 校验扩展字段类型
     *
     * @param field
     * @return
     */
    public static ServiceResult<String> checkFieldValueFormat(HttpMemberFieldParam field) {
        Integer type = field.getType();
        Object fieldValue = field.getValue();
        if (ObjectUtil.isEmpty(fieldValue)) {
            return ServiceResult.success();
        }
        switch (ExtendFieldTypeEnum.getByIndex(type)) {
            case NUMBER:
                String number = String.valueOf(fieldValue);
                return CheckUtil.checkNumberFormat(number);
            case MOBILE:
                String mobile = String.valueOf(fieldValue);
                return CheckUtil.checkMobilePhoneFormat(mobile);
            case EMAIL:
                String email = String.valueOf(fieldValue);
                return CheckUtil.checkEmailFormat(email);
            default:
                return ServiceResult.success();
        }
    }
}
